/*******************************************************************************
 * Copyright (c) 2009-2011 ETCORE Labs. All rights reserved. 
 * 
 * This software is the confidential and proprietary information of ETCORE Labs.
 * You shall not disclose such confidential information and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with ETCORE Labs. 
 * 
 * Support can be obtained from support forums at:  http://www.etcore.com/
 * /
 *******************************************************************************/
package etcore.webapp.grouptalk.user.service;

import java.util.List;

import org.hibernate.Session;

import etcore.webapp.grouptalk.common.dao.HibernateSessionFactory;
import etcore.webapp.grouptalk.common.service.BaseService;
import etcore.webapp.grouptalk.user.dao.FocusDAO;
import etcore.webapp.grouptalk.user.object.Focus;

/**
 * FocusService implements queries and transactions on Focus
 * 
 * @author ETCORE GroupTalk Developers
 * 
 */
public class FocusService extends BaseService {
	/**
	 * Find Focus instances
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            count of Focus instances
	 * @return Focus instances
	 */
	public List<Focus> findAll(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			List<Focus> result = dao.findAll(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

 
	public List<Focus> findFocuses(int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			List<Focus> result = dao.findFocuses(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the count of Focus instances
	 * 
	 * @return count of Focus instances
	 */
	public long findAllSize() {

		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			long result = dao.findAllSize();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findAllFollowedSizeBetweenAdmins() {

		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			long result = dao.findAllFollowedSizeBetweenAdmins();

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findAllFollowedSizeBetweenAdmins(String inputDate) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			long result = dao.findAllFollowedSizeBetweenAdmins(inputDate);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeOfNotFollowedAdmin(String fromUser) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			long result = dao.findSizeOfNotFollowedAdmin(fromUser);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public long findSizeOfFollowedAdmin(String fromUser, String date) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			long result = dao.findSizeOfFollowedAdmin(fromUser, date);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	public long findAdminsSizeByMaxHisFollowed(long maxFollowed){

		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			long result = dao.findAdminsSizeByMaxHisFollowed(maxFollowed);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
	 
	public List<Object[]>  findAdminsByMaxHisFollowed(int maxFollowed,
			int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			List<Object[]> result = dao.findAdminsByMaxHisFollowed(
					maxFollowed, begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<String> findNotFollowedAdmins(String fromUser, int begin,
			int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			List<String> result = dao.findNotFollowedAdmins(fromUser, begin,
					length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Focus instances by fromUser and toUser
	 * 
	 * @param fromUser
	 *            the person who follows toUser
	 * @param toUser
	 *            the person who is followed by fromUser
	 * @return Focus instance
	 */
	public Focus findBy2Properties(String fromUser, String toUser) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			Focus result = dao.findBy2Properties(fromUser, toUser);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Focus instance by id
	 * 
	 * @param id
	 *            Focus id
	 * @return Focus instance
	 */
	public Focus findById(java.lang.Integer id) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			Focus result = dao.findById(id);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find Focus instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return Focus instances
	 */
	public List<Focus> findByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			List<Focus> result = dao.findByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find instances of Focus by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @param begin
	 *            start position
	 * @param length
	 *            count of instances
	 * @return Focus instances
	 */
	public List<Focus> findByProperty(String propertyName, Object value,
			int begin, int length) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			List<Focus> result = dao.findByProperty(propertyName, value, begin,
					length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find the count of Focus instances by a property value
	 * 
	 * @param propertyName
	 *            property name
	 * @param value
	 *            property value
	 * @return count of Focus instances
	 */
	public long findSizeByProperty(String propertyName, Object value) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			long result = dao.findSizeByProperty(propertyName, value);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}

	/**
	 * Find those with most followers.
	 * 
	 * @param begin
	 *            start position
	 * @param length
	 *            count of
	 * @return a list of object array,which contains toUser and the count of
	 *         followers.
	 */
	@SuppressWarnings("rawtypes")
	public List findUserStars(int begin, int length) {

		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			List result = dao.findUserStars(begin, length);

			return result;
		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}
	}

	/**
	 * Check whether a Focus instance with fromUser and toUser exists
	 * 
	 * @param fromUser
	 *            the person who follows toUser
	 * @param toUser
	 *            the person who is followed by fromUser
	 * @return true or false
	 */
	public boolean isAdded(String fromUser, String toUser) {
		Session session = HibernateSessionFactory.getSession();
		try {
			FocusDAO dao = new FocusDAO();
			Object result = dao.findBy2Properties(fromUser, toUser);
			if (result != null)
				return true;
			return false;

		} catch (RuntimeException re) {

			throw re;
		} finally {
			if (session != null)
				session.close();
		}

	}
}